/**
 * Created by Admin on 07.01.2015.
 */

var Surf = (function ($) {

    var SURF = $.inherit(GlColoredSceneObject, {
        options: {
            sizeX: 15,
            sizeY: 100,
            minZ: 0,
            maxZ: 5,
            colorMap: ColorMap.Jet
        },

        getZ: function (datum, minZ, maxZ) {
            return Math.rescale(datum, minZ, maxZ, SURF.MIN_Z, SURF.MAX_Z);
        },

        getColor: function (datum, minZ, maxZ) {
            return this.options.colorMap.getColor(datum, minZ, maxZ)
        },

        __constructor: function (gl, data, options) {
            this.options = $.extend({}, this.options, options);

            var sizeX = this.options.sizeX;
            var sizeY = this.options.sizeY;

            var minZ = this.options.minZ;
            var maxZ = this.options.maxZ;
            var vertices = [];
            var indices = [];
            var colors = [];
            for (var lcY = 0; lcY < sizeY; lcY++) {
                for (var lcX = 0; lcX < sizeX; lcX++) {
                    var x = Math.rescale(lcX, 0, sizeX, SURF.MIN_X, SURF.MAX_X);
                    var y = Math.rescale(lcY, 0, sizeY, SURF.MIN_Y, SURF.MAX_Y);
                    var datum = (data[lcY] && data[lcY][lcX]) || 0;
                    var z = this.getZ(datum, minZ, maxZ);
                    vertices = $.merge(vertices, [x, z, y]);
                    var current = (lcY * sizeX + lcX);
                    var next = ((lcY + 1) * sizeX + lcX);
                    lcX < sizeX - 1 && lcY < sizeY - 1 && (indices = $.merge(indices, [current, current + 1, next + 1, current, next + 1, next]));
                    colors = $.merge(colors, this.getColor(datum, minZ, maxZ));
                }
            }

            this.__base(gl, vertices, colors, indices);
        }
    }, {
        MIN_X: 0,
        MAX_X: 2,

        MIN_Y: 0,
        MAX_Y: 2,

        MIN_Z: -1,
        MAX_Z: 1
    });
    return SURF
})(window["jQuery"]);

var FuncSurf = (function ($) {
    var FUNC_SURF = $.inherit(Surf, {
        __constructor: function (gl, func, options) {
            options = $.extend({}, {
                minX: 0,
                maxX: 10,

                minY: 0,
                maxY: 10
            }, options);

            var data = [];
            var minX = options.minX;
            var maxX = options.maxX;
            var minY = options.minY;
            var maxY = options.maxY;
            var minZ = Number.MAX_VALUE;
            var maxZ = -Number.MAX_VALUE;
            var gridX = FUNC_SURF.GRID_X;
            var gridY = FUNC_SURF.GRID_Y;
            for (var lcY = 0; lcY < gridY; lcY++) {
                data[lcY] = [];
                var y = Math.rescale(lcY, 0, gridY, minY, maxY);
                for (var lcX = 0; lcX < FUNC_SURF.GRID_X; lcX++) {
                    var x = Math.rescale(lcX, 0, gridX, minX, maxX);
                    var v = func(x, y);
                    v > maxZ && (maxZ = v);
                    v < minZ && (minZ = v);
                    data[lcY][lcX] = v;

                }
            }

            this.__base(gl, data, $.extend(options, {
                minZ: minZ,
                maxZ: maxZ,
                sizeX: gridX,
                sizeY: gridY
            }));
        }
    }, {
        GRID_X: 100,
        GRID_Y: 100
    });
    return FUNC_SURF
})(window["jQuery"]);